package com.own.business.system.user.controller.manage;

import com.own.aop.permission.PermissionModule;
import com.own.aop.permission.PermissionOperation;
import com.own.business.system.user.business.service.SystemUserInfoService;
import com.own.business.system.user.common.constant.ConstantModule;
import com.own.business.system.user.controller.manage.form.UpdateUserAvatarForm;
import com.own.business.system.user.entity.query.SystemUserInfoQuery;
import com.own.business.system.user.entity.vo.SystemUserInfoVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.model.PageModel;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import com.own.constant.ConstantPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ManageSystemUserInfoController
 *
 * @author chenxueli
 * @date 2024-03-23 21:27:33
 */
@Tag(name = "系统用户" + ConstantBeanSuffix.MANAGE_CLIENT + ConstantBeanSuffix.CONTROLLER)
@PermissionModule(ConstantModule.USER)
@RestController
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.MANAGE_CLIENT + "system/user/info")
public class ManageSystemUserInfoController extends BaseController {

    @Resource
    private SystemUserInfoService systemUserInfoService;

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "分页查询用户信息")
    @PostMapping("/page")
    public ResultModel<PageModel<SystemUserInfoVo>> page(
            @RequestBody SystemUserInfoQuery query
    ) {
        var pageModel = systemUserInfoService.page(query);
        return R.success(pageModel);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.UPDATE)
    @Operation(summary = "根据用户id修改头像")
    @PutMapping("/update/avatar")
    public ResultModel<Boolean> updateSelfAvatar(
            @RequestBody UpdateUserAvatarForm form
    ) {
        form.check();
        systemUserInfoService.updateAvatar(form.getUserId(), form.getAvatar());
        return R.success(true);
    }

    @LogOperationAop
    @PermissionOperation(ConstantPermission.SEARCH)
    @Operation(summary = "根据用户id获取信息")
    @GetMapping("/user/id/{userId}")
    public ResultModel<SystemUserInfoVo> getByUserId(
            @Parameter(description = "主键id", required = true) @PathVariable Long userId
    ) {
        var po = systemUserInfoService.getByUserId(userId);
        if (po == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        return R.success(new SystemUserInfoVo(po));
    }

}
